package com.lottery.ruok.lottrey.fragment;

import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.JsonArray;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.PrizeListActivity;
import com.lottery.ruok.lottrey.adapter.LongDragonAdapter;
import com.lottery.ruok.lottrey.bean.LongDragonItem;
import com.lottery.ruok.lottrey.eventbusObject.Tabs;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.LotteryStatThreeResquest;
import com.lottery.ruok.lottrey.retrofitModule.response.LotteryStatOneResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.LotteryStatThreeResponse;
import com.lottery.ruok.lottrey.ui.MyGridView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ASUS on 2017/7/27.
 */

public class LongDragonFragment extends BaseFragment {
    @BindView(R.id.grid_view)
    MyGridView gridView;

    private ArrayList<LongDragonItem> items = new ArrayList<>();
    private LongDragonAdapter longDragonAdapter;
    private LotteryStatOneResponse data;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.long_dragon_fragment, container, false);

        ButterKnife.bind(this, view);

        init();
        return view;
    }

    private void init() {
        getList();
    }

    public void getList(){
        items.clear();
        if((getActivity()) != null) {
            RetrofitBase.getRetrofit().create(LotteryStatThreeResquest.class)
                    .getResult(((PrizeListActivity)getActivity()).currentCategory, ((PrizeListActivity)getActivity()).currentDate)
                    .compose(RxHelper.<LotteryStatThreeResponse>handleResult())
                    .subscribe(new RxSubscribe<LotteryStatThreeResponse>(getActivity()) {
                        @Override
                        protected void _onNext(LotteryStatThreeResponse lotteryStatThreeResponse) {
                            if((getActivity()) != null && isAdded()) {
                                String unit = "球";
                                if (((PrizeListActivity) getActivity()).currentCategory != null) {
                                    if (((PrizeListActivity) getActivity()).currentCategory.equals("bjpk10") || ((PrizeListActivity) getActivity()).currentCategory.equals("ctbjpk10")) {
                                        unit = "名";
                                    }
                                }
                                if (lotteryStatThreeResponse.getZonghedaxiao() != null) {
                                    int temp = getSum(lotteryStatThreeResponse.getZonghedaxiao(), "大");
                                    if (temp != 0) {
                                        Spanned item = Html.fromHtml("总和大小:大<font color='#ff6c2c'>" + temp + "</font>期");
                                        items.add(new LongDragonItem(item, temp));
                                    }
                                    temp = getSum(lotteryStatThreeResponse.getZonghedaxiao(), "小");
                                    if (temp != 0) {
                                        Spanned item1 = Html.fromHtml("总和大小:小<font color='#ff6c2c'>" + temp + "</font>期");
                                        items.add(new LongDragonItem(item1, temp));
                                    }
                                }
                                if (lotteryStatThreeResponse.getZonghedanshuang() != null) {
                                    int temp = getSum(lotteryStatThreeResponse.getZonghedanshuang(), "单");
                                    if (temp != 0) {
                                        Spanned item = Html.fromHtml("总和单双:单<font color='#ff6c2c'>" + temp + "</font>期");
                                        items.add(new LongDragonItem(item, temp));
                                    }
                                    temp = getSum(lotteryStatThreeResponse.getZonghedanshuang(), "双");
                                    if (temp != 0) {
                                        Spanned item1 = Html.fromHtml("总和单双:双<font color='#ff6c2c'>" + temp + "</font>期");
                                        items.add(new LongDragonItem(item1, temp));
                                    }
                                }
                                if (lotteryStatThreeResponse.getLonghuhe() != null && lotteryStatThreeResponse.getLonghuhe().size() > 0) {
                                    if (lotteryStatThreeResponse.getLonghuhe().get(0).isJsonArray()) {//判断第一个元素是否是数组即可
                                        for (int i = 0; i < lotteryStatThreeResponse.getLonghuhe().size(); i++) {
                                            int temp = getSum(lotteryStatThreeResponse.getLonghuhe().get(i).getAsJsonArray(), "龙");
                                            if (temp != 0) {
                                                Spanned item = Html.fromHtml("第" + (i + 1) + unit + ":龙<font color='#ff6c2c'>" + temp + "</font>期");
                                                items.add(new LongDragonItem(item, temp));
                                            }
                                            temp = getSum(lotteryStatThreeResponse.getLonghuhe().get(i).getAsJsonArray(), "虎");
                                            if (temp != 0) {
                                                Spanned item1 = Html.fromHtml("第" + (i + 1) + unit + ":虎<font color='#ff6c2c'>" + temp + "</font>期");
                                                items.add(new LongDragonItem(item1, temp));
                                            }
                                            temp = getSum(lotteryStatThreeResponse.getLonghuhe().get(i).getAsJsonArray(), "和");
                                            if (temp != 0) {
                                                Spanned item2 = Html.fromHtml("第" + (i + 1) + unit + ":和<font color='#ff6c2c'>" + temp + "</font>期");
                                                items.add(new LongDragonItem(item2, temp));
                                            }
                                        }
                                    } else {
                                        int temp = getSum(lotteryStatThreeResponse.getLonghuhe(), "龙");
                                        if (temp != 0) {
                                            Spanned item = Html.fromHtml("龙虎和:龙<font color='#ff6c2c'>" + temp + "</font>期");
                                            items.add(new LongDragonItem(item, temp));
                                        }
                                        temp = getSum(lotteryStatThreeResponse.getLonghuhe(), "虎");
                                        if (temp != 0) {
                                            Spanned item1 = Html.fromHtml("龙虎和:虎<font color='#ff6c2c'>" + temp + "</font>期");
                                            items.add(new LongDragonItem(item1, temp));
                                        }
                                        temp = getSum(lotteryStatThreeResponse.getLonghuhe(), "和");
                                        if (temp != 0) {
                                            Spanned item2 = Html.fromHtml("龙虎和:和<font color='#ff6c2c'>" + temp + "</font>期");
                                            items.add(new LongDragonItem(item2, temp));
                                        }
                                    }
                                }
                                if (lotteryStatThreeResponse.getDaxiao() != null) {
                                    for (int i = 0; i < lotteryStatThreeResponse.getDaxiao().size(); i++) {
                                        int temp = getSum(lotteryStatThreeResponse.getDaxiao().get(i), "大");
                                        if (temp != 0) {
                                            Spanned item = Html.fromHtml("第<font color='#ff6c2c'>" + (i + 1) + "</font>" + unit + ":大<font color='#ff6c2c'>" + temp + "</font>期");
                                            items.add(new LongDragonItem(item, temp));
                                        }
                                        temp = getSum(lotteryStatThreeResponse.getDaxiao().get(i), "小");
                                        if (temp != 0) {
                                            Spanned item1 = Html.fromHtml("第<font color='#ff6c2c'>" + (i + 1) + "</font>" + unit + ":小<font color='#ff6c2c'>" + temp + "</font>期");
                                            items.add(new LongDragonItem(item1, temp));
                                        }
                                    }
                                }
                                if (lotteryStatThreeResponse.getDanshuang() != null) {
                                    for (int i = 0; i < lotteryStatThreeResponse.getDanshuang().size(); i++) {
                                        int temp = getSum(lotteryStatThreeResponse.getDanshuang().get(i), "单");
                                        if (temp != 0) {
                                            Spanned item = Html.fromHtml("第<font color='#ff6c2c'>" + (i + 1) + "</font>" + unit + ":单<font color='#ff6c2c'>" + temp + "</font>期");
                                            items.add(new LongDragonItem(item, temp));
                                        }
                                        temp = getSum(lotteryStatThreeResponse.getDanshuang().get(i), "双");
                                        if (temp != 0) {
                                            Spanned item1 = Html.fromHtml("第<font color='#ff6c2c'>" + (i + 1) + "</font>" + unit + ":双<font color='#ff6c2c'>" + temp + "</font>期");
                                            items.add(new LongDragonItem(item1, temp));
                                        }
                                    }
                                }
                                if (lotteryStatThreeResponse.getWeishudaxiao() != null) {
                                    for (int i = 0; i < lotteryStatThreeResponse.getWeishudaxiao().size(); i++) {
                                        int temp = getSum(lotteryStatThreeResponse.getWeishudaxiao().get(i), "大");
                                        if (temp != 0) {
                                            Spanned item = Html.fromHtml("尾数大小:第<font color='#ff6c2c'>" + (i + 1) + "</font>" + unit + ":大<font color='#ff6c2c'>" + temp + "</font>期");
                                            items.add(new LongDragonItem(item, temp));
                                        }
                                        temp = getSum(lotteryStatThreeResponse.getWeishudaxiao().get(i), "小");
                                        if (temp != 0) {
                                            Spanned item1 = Html.fromHtml("尾数大小:第<font color='#ff6c2c'>" + (i + 1) + "</font>" + unit + ":小<font color='#ff6c2c'>" + temp + "</font>期");
                                            items.add(new LongDragonItem(item1, temp));
                                        }
                                    }
                                }
                                if (lotteryStatThreeResponse.getHeshudanshuang() != null) {
                                    for (int i = 0; i < lotteryStatThreeResponse.getHeshudanshuang().size(); i++) {
                                        int temp = getSum(lotteryStatThreeResponse.getHeshudanshuang().get(i), "单");
                                        if (temp != 0) {
                                            Spanned item = Html.fromHtml("和数单双:第<font color='#ff6c2c'>" + (i + 1) + "</font>" + unit + ":单<font color='#ff6c2c'>" + temp + "</font>期");
                                            items.add(new LongDragonItem(item, temp));
                                        }
                                        temp = getSum(lotteryStatThreeResponse.getHeshudanshuang().get(i), "双");
                                        if (temp != 0) {
                                            Spanned item1 = Html.fromHtml("和数单双:第<font color='#ff6c2c'>" + (i + 1) + "</font>" + unit + ":双<font color='#ff6c2c'>" + temp + "</font>期");
                                            items.add(new LongDragonItem(item1, temp));
                                        }
                                    }
                                }
                                if (lotteryStatThreeResponse.getZongheweishudaxiao() != null) {
                                    int temp = getSum(lotteryStatThreeResponse.getZongheweishudaxiao(), "大");
                                    if (temp != 0) {
                                        Spanned item = Html.fromHtml("总和尾数大小:大<font color='#ff6c2c'>" + temp + "</font>期");
                                        items.add(new LongDragonItem(item, temp));
                                    }
                                    temp = getSum(lotteryStatThreeResponse.getZongheweishudaxiao(), "小");
                                    if (temp != 0) {
                                        Spanned item1 = Html.fromHtml("总和尾数大小:小<font color='#ff6c2c'>" + temp + "</font>期");
                                        items.add(new LongDragonItem(item1, temp));
                                    }
                                }

                                Comparator c = new Comparator<LongDragonItem>() {
                                    @Override
                                    public int compare(LongDragonItem o1, LongDragonItem o2) {
                                        if (o1.getNumber() < o2.getNumber()) {
                                            return 1;
                                        } else if (o1.getNumber() == o2.getNumber()) {
                                            return 0;
                                        } else return -1;
                                    }
                                };
                                Collections.sort(items, c);
                                longDragonAdapter = new LongDragonAdapter(getActivity(), items);
                                gridView.setAdapter(longDragonAdapter);
                            }
                        }

                        @Override
                        protected boolean showDialog() {
                            return false;
                        }
                    });
        }
    }

    private int getSum(List<String> s, String target) {
        int all = 0;
        for (int i = s.size() - 1; i > 0; i--) {
            if (s.get(i).equals(target)) {
                all++;
            } else {
                return all;
            }
        }
        return all;
    }

    private int getSum(JsonArray s, String target) {
        int all = 0;
        if(s != null) {
            for (int i = s.size() - 1; i > 0; i--) {
                if (s.get(i) != null && s.get(i).getAsString().equals(target)) {
                    all++;
                } else {
                    return all;
                }
            }
        }
        return all;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setTabs(Tabs tabs) {
        if (tabs.isCanListScroll()) {
            gridView.setCanListScroll(true);
        } else {
            gridView.setCanListScroll(false);
        }
    }

    public void setData(LotteryStatOneResponse data) {
        this.data = data;
        getList();
    }
}